﻿<#
.Synopsis
   Downloads the daily image or video from Bing and sets it as your wallpaper
.DESCRIPTION
   Set this up as a scheduled task and it will download the daily image or video from bing

   You need VLC player installed to play the video of the day

   See examples of what the image of the day is across the countries:
   http://www.istartedsomething.com/bingimages/

   List of codes here:
   https://msdn.microsoft.com/en-gb/library/ee825488(v=cs.20).aspx
.EXAMPLE
    Play the bing video of the day (if there is one)  The wallpaper is always set behind anyway.

   Set-BingImageVideo $true
.EXAMPLE
   Just set the bing wallpaper of the day, do not play the video

   Set-BingImageVideo $false
.EXAMPLE
    Set the country code to Spain

    Set-BingImageVideo $false es-ES
#>
function Set-BingImageVideo
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # PlayVideo - Plays the daily video (if true and there is one!)
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [bool]
        $PlayVideo = $false,

        # Sometimes the image of the day varies by country, set your country code (en-GB, en-US etc)
        
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        $CountryCode = "en-GB"
    )

    Begin
    {
    }
    Process
    {
    
        $doc = New-Object System.Xml.XmlDocument
        $doc.Load("http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=" + $CountryCode + "&video=1")        

        # Is there a video today?
        $video = $doc.images.image.video.codecs.codec.url

        if ($video -and $PlayVideo)
        {
            # Display video
            $wallpaperUrl = $video.Replace('//', 'http://')

            # Download video
            $wc = New-Object System.Net.WebClient
            $wc.DownloadFile($wallpaperUrl, 'C:\temp\BingVideoOfTheDay.mp4')

            # Set Wallpaper
            cd "C:\Program Files (x86)\VideoLAN\VLC"
            .\vlc.exe --fullscreen --no-video-title-show --no-embedded-video --no-qt-fs-controller --qt-start-minimized --qt-system-tray --video-wallpaper --no-audio --qt-notification=0 --loop "file:///c:/Temp/BingVideoOfTheDay.mp4"

        }

        # Display the image
        $wallpaperUrl = 'http://www.bing.co.uk' + $doc.images.image.url

        $wc = New-Object System.Net.WebClient
        $wc.DownloadFile($wallpaperUrl, 'C:\temp\BingPicOfTheDay.jpg')


        # Set Wallpaper
        Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value 'C:\temp\BingPicOfTheDay.jpg'

        # Refresh wallpaper
        #rundll32.exe user32.dll, UpdatePerUserSystemParameters,1,True

        Add-Type -TypeDefinition @" 
        using System; 
        using System.Runtime.InteropServices;

        public class Params
        { 
            [DllImport("User32.dll",CharSet=CharSet.Unicode)] 
            public static extern int SystemParametersInfo (Int32 uAction, 
                                                            Int32 uParam, 
                                                            String lpvParam, 
                                                            Int32 fuWinIni);
        }
"@ 

        $SPI_SETDESKWALLPAPER = 0x0014
        $UpdateIniFile = 0x01
        $SendChangeEvent = 0x02

        $fWinIni = $UpdateIniFile -bor $SendChangeEvent 

        $ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, "C:\temp\BingPicOfTheDay.jpg", $fWinIni)

    }
    End
    {
    }
}



# Run the code
Set-BingImageVideo $false en-GB